function [ boolean ] = cut_criteria( generation, population, previous_population, best_fitness )
% CUT_CRITERIA
%   Esta funcion determina, de acuerdo al metodo de corte seleccionado en
%   constants.m, si el algoritmo ha finalizado o no.

global CUT_CRITERIA;
global MAX_GENERATIONS;
global FITNESS_DESIRED;
global MAX_GEN_STRUCTURE;

boolean = true;
if CUT_CRITERIA == 1
    if generation == MAX_GENERATIONS
        boolean = false;
    end
elseif CUT_CRITERIA == 2
    equals = 0;
    for i = 1:length(population)-1
       previous_equals = equals;
       for j = i+1:length(population)
           if i ~= j && ~isempty(population{i})
               if isequal(population{i}, population{j})
                   equals = equals+1;
                   population{j} = [];
               end
           end
       end
       if previous_equals ~= equals
           population{i} = [];
           equals = equals+1;
       end
    end
    if equals / length(population) > 0.6
        boolean = false;
    end
elseif CUT_CRITERIA == 3 && generation > 1
    f = zeros(1, length(previous_population));
    for i = 1:length(previous_population)
        f(i) = fitness(previous_population{i});
    end
    if max(f) >= best_fitness
        MAX_GEN_STRUCTURE = MAX_GEN_STRUCTURE + 1;
    else
        MAX_GEN_STRUCTURE = 0;
    end
    if MAX_GEN_STRUCTURE == 10
        boolean = false;
    end
else
    f = zeros(1, length(population));
    for i = 1:length(population)
        f(i) = fitness(population{i}); 
    end
    if max(f) >= FITNESS_DESIRED
        boolean = false;
    end
end

end
